package wypozyczalnia.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import wypozyczalnia.domain.Hire;
import wypozyczalnia.domain.User;
import wypozyczalnia.forms.RentForm;
import wypozyczalnia.service.HireService;
import wypozyczalnia.service.UserService;

@Controller
@RequestMapping(value = "/ownHires/*")
public class OwnHiresController {
	private final Logger logger = Logger.getLogger(ProfileController.class);
	@Autowired
	HireService hireService;

	@Autowired
	UserService userService;

	@RequestMapping(value = "list.do")
	public String list(HttpSession session, HttpServletResponse response,
			HttpServletRequest request, Model model) {
		User user = getCurrentUser(session);
		List<Hire> hires = hireService.getHiresForUser(user);
		model.addAttribute("hires", hires);
		return "ownHiresList";
	}

	@RequestMapping(value = "details.do")
	public String details(HttpSession session, HttpServletResponse response,
			HttpServletRequest request, Model model, RentForm rentForm) {

		Hire hire = hireService.getHireById(rentForm.getId());
		model.addAttribute("hire", hire);
		return "ownHireDetails";
	}
	
	@RequestMapping(value = "complaint.do")
	public String complaint(HttpSession session, HttpServletResponse response,
			HttpServletRequest request, Model model, RentForm rentForm) {

		Hire hire = hireService.getHireById(rentForm.getId());
		model.addAttribute("hire", hire);
		return "addComplaint";
	}

	private User getCurrentUser(HttpSession session) {
		User user = (User) userService.getUserByUsername((String) session
				.getAttribute("currentUserName"));
		return user;
	}

}
